//
//  LLGComposeToolBar.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/17.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit

//按钮类型
enum LLGComposeToolBarButtonType: Int {

    //图片
    case Picture = 0
    //@
    case Mention = 1
    //#
    case Trend = 2
    //表情
    case Emoticon = 3
    //加号
    case Add = 4
}

//自定义发微博toolBar
class LLGComposeToolBar: UIStackView {
    
    var emoticonButton: UIButton?
    
    //按钮点击闭包回调
    var didSelectedToolBarButtonClosure: ((type: LLGComposeToolBarButtonType) -> ())?
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: --添加控件,设置约束
    private func setupUI() {
        
        //水平方向布局
        axis = .Horizontal
        //等比填充
        distribution = .FillEqually
    
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
        
    }
    
    // MARK: --添加子控件方法
    private func addChildButton(imageName: String, type: LLGComposeToolBarButtonType) -> UIButton {
    
        let button = UIButton()
        
        //枚举原始值
        button.tag = type.rawValue
        
        //点击事件
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        
        //取消高亮状态
        button.adjustsImageWhenHighlighted = false
        
        //添加子控件
        addArrangedSubview(button)
        
        return button
    }
    
    // MARK: --按钮点击事件
    @objc private func buttonAction(button: UIButton) {
    
        //根据原始值获取枚举值
        let type = LLGComposeToolBarButtonType(rawValue: button.tag)!
        
        didSelectedToolBarButtonClosure?(type: type)
    }
    
    func swichEmoticon(isEmoticon: Bool) {
    
        if isEmoticon {
        
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
        } else {
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
    }
    
}
